package com.mmall.controller;


import com.alibaba.fastjson.JSONArray;


import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Goods;
import com.mmall.pojo.User;
import com.mmall.service.GoodsService;
import com.mmall.service.IFileService;
import com.mmall.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Controller
@RequestMapping("goods/")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserService userService;
    @Autowired
    private IFileService fileService;

    @RequestMapping(value = "create.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse create(Goods goods,String loginToken, String[] picArrary) {
        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        User user = (User) response.getData();
        goods.setUserId(user.getId());
        if (picArrary != null && picArrary.length > 0) {
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < picArrary.length; i++) {
                jsonArray.add(picArrary[i]);
            }
            goods.setCreateTime(new Date());
            goods.setPicture(jsonArray.toString());
        }
        return goodsService.insertGoods(goods);
    }

    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse update(Goods goods,String loginToken) {
        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        return goodsService.updateGoods(goods);
    }


    @RequestMapping(value = "find.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse find(Integer id,String loginToken) {
        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        User user = (User) response.getData();
        return goodsService.selectGoods(id,user.getId());
    }

    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse delete(Integer id,String loginToken) {
        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        return goodsService.deleteGoods(id);
    }

    @RequestMapping(value = "findGoodsList.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse findGoodsList(String loginToken) {
        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        User user = (User) response.getData();
        return goodsService.findGoodsListByUserId(user.getId());
    }


    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse list() {
        return goodsService.findAll();
    }

    @RequestMapping(value = "upload.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) {
        String path = request.getSession().getServletContext().getRealPath("upload");
        return fileService.uploadFile(file, path, Const.GOODS_IMG);
    }

    @RequestMapping(value = "findGoodsWithAddress.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse findGoodsWithAddress(Integer id,String loginToken){
        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        User user = (User) response.getData();
        return goodsService.findGoodsWithDefaultAdress(id,user.getId());
    }

    @RequestMapping(value = "collectionGoods.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse collectionGoods(Integer goodsId,String loginToken){
        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        User user = (User) response.getData();
        return goodsService.collectionGoods(user.getId(),goodsId);
    }

    @RequestMapping(value = "cancelCollectionGoods.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse cancelCollectionGoods(Integer goodsId,String loginToken){
        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        User user = (User) response.getData();
        return goodsService.cancelCollectionGoods(user.getId(),goodsId);
    }

    @RequestMapping(value = "getListByGoodsTye.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getListByGoodsTye(String goodsType){
        return goodsService.selectListByGoodsType(goodsType);
    }

    @RequestMapping(value = "findListByCampus.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse findListByCampus(String campus){
        Goods goods = new Goods();
        goods.setCampus(campus);
        return goodsService.findListByEntity(goods);
    }

    @RequestMapping(value = "findCollectionList.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse findCollectionList(String loginToken){
        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        User user = (User) response.getData();
        return goodsService.findCollectionList(user.getId());
    }
}
